from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.db.models import permalink

from pennypress.apps.categories.managers import CategoryManager


class Category(models.Model):
    title = models.CharField(max_length=50,help_text=_('this is the name that will appear in the section'))
    category_slug = models.CharField(_('category'),max_length=30,unique=True,help_text=_('no slashes needed'))
    parent = models.ForeignKey('self', verbose_name=_('Parent Category'), \
        null=True, blank=True, help_text=_('Do note leave blank unless you want a entirly new root category'))
    slug = models.SlugField(blank=True, editable=False, unique=True)
    sort_order = models.IntegerField(_('Sort Order'), default=0)
    
    objects = CategoryManager()
    
    def get_children(self):
        self.objects.filter(parent = self)

    def __unicode__(self):
        return '%s [%s]' % (self.title, self.slug)

    def get_absolute_url(self):
        return self.slug

    def save(self):
        if self.parent is None:
            self.slug = '%s/' % (self.slug)
        else:
            self.slug = '%s/%s/' % (self.parent, self.slug)
        super(Category, self).save()

    def get_absolute_url(self):
        return ('category_detail', self.slug)
    get_absolute_url = permalink(get_absolute_url)
    
    class Meta:
        verbose_name="Category"
        verbose_name_plural="Categories"

    class Admin:
        pass


class Section(models.Model):
    """
    The section object that declares where an object should should live
    A section has a relation to a single category, but an object could 
    be in multiple categories
    """
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=100)
    url = models.SlugField(blank=True, null=True, help_text=_("Example: 'news/'. The slug defaults to the slug of the category"))
    template_name = models.CharField(max_length=70, default='section/default.html', help_text=_("Example: 'section/news.html'."))

    def __unicode__(self):
        return self.title
    
    def get_absolute_url(self):
        return ('section_detail', self.category.slug)
    get_absolute_url = permalink(get_absolute_url)
    
    def save(self):
        if self.url == '':
            self.url = self.category.slug
        super(Section, self).save()
        
    class Admin:
        pass            


class CategoryBase(models.Model):
    """A base class that allows for traversal of categories"""
    categories = models.ManyToManyField(Category, verbose_name=_('Categories'), blank=True, null=True)
        
    class Meta:
        abstract = True


class SectionBase(models.Model):
    section = models.ForeignKey(Section, verbose_name=_('Section'))

    class Meta:
        abstract = True


try:
    from django.contrib import admin

    class CategoryAdmin(admin.ModelAdmin):
        pass

    class SectionAdmin(admin.ModelAdmin):
        pass

    admin.site.register(Category, CategoryAdmin)
    admin.site.register(Section, SectionAdmin)

except:
    pass

